home *** CD-ROM | disk | FTP | other *** search
/ POINT Software Programming / PPROG1.ISO / pascal / swag / crc.swg / 0008_16 Bit CRC.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1993-08-27  |  1.5 KB  |  45 lines

  1. {
  2. GUY MCLOUGHLIN
  3.  
  4. >I wanted to ask you... would you happen to know how a CRC Check-sum
  5. >works? Everytime I go to look this up in a book I see a bunch of
  6. >stuff about X^7 + X^12 + X^17..... (and on and on) but nothing that
  7. >actually says "Here's what the code looks like" ... just a bunch of
  8. >non-sensical bull...Would you happen to know the algorithm that is
  9. >used?
  10.  
  11.   ...Greg Vigneault is much better at this stuff than I am. I
  12.   usually know "why" something works, but not always "how". <g>
  13.   The basic idea is that the data is treated as input to a specific
  14.   polynomial equation (ie: X^32 + X^26 + X^23 + X^22 + X^16 + X^12),
  15.   the result of this is then divided by a specific prime number, and
  16.   the remainder left over is the CRC value. I know that this is
  17.   easier said than understood, but that's the gist of it.
  18.  
  19.   ...if a single bit of a chunk of data is changed, the chances
  20.   are very good that a CRC check number would catch this change.
  21.   It's not 100 percent guaranteed, but something more like 99.97
  22.   percent, so CRCs are not an entirely bulletproof check. Here's
  23.   a standard Pascal Implementation of a CRC-16 routine:
  24. }
  25.  
  26. Function CRC16(InString: String) : Word;
  27. Var
  28.   CRC     : Word;
  29.   Index1,
  30.   Index2  : Byte;
  31. begin
  32.   CRC := 0;
  33.   For Index1 := 1 to length(S) do
  34.   begin
  35.     CRC := (CRC xor (ord(InString[Index1]) SHL 8));
  36.     For Index2 := 1 to 8 do
  37.       if ((CRC and $8000) <> 0) then
  38.         CRC := ((CRC SHL 1) xor $1021)
  39.       else
  40.         CRC := (CRC SHL 1)
  41.   end;
  42.   CRC16 := (CRC and $FFFF)
  43. end;
  44.  
  45.